﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using System.IO;
using System.Net;
using log4net.Core;
using log4net;

namespace DWH.PP.Recursive.ISTOR
{
    public class ISTORPullTask : PPRecursiveBoardPullTask
    {
        private ILog logger;

        public ISTORPullTask()
        {
            logger = LogManager.GetLogger(typeof(ISTORPullTask));
        }

        public override string ProcessTargetHtmlContent(string pageUrl, string targetHtmlContent)
        {
            //<a href="http://www.new080.com/food/106">
            //http://www.new080.com/Food/106/links.txt

            string downloadLinks = null;

            try
            {
                Match m = Regex.Match(targetHtmlContent, @"http://www.new080.com/food/\d+", RegexOptions.IgnoreCase);
                if (m != null && !String.IsNullOrEmpty(m.Value))
                {
                    WebClient wc = new WebClient();
                    downloadLinks = wc.DownloadString(String.Format("{0}/{1}", m.Value, "links.txt"));
                    //downloadLinks = File.ReadAllText(String.Format("{0}/{1}", m.Value, "links.txt"));
                }
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("task {0}, exception occures when trying to processing url {1} targetHtmlContent:{2}, exception:{3}", TaskName, pageUrl, targetHtmlContent, ex.ToString());
            }

            if (!String.IsNullOrEmpty(downloadLinks))
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0}<div>{1}</div>", targetHtmlContent, downloadLinks);

                return sb.ToString();
            }
            else
            {
                return base.ProcessTargetHtmlContent(pageUrl, targetHtmlContent);
            }
        }        
    }
}
